/*
 * JaLingo, http://jalingo.sourceforge.net/
 *
 * Copyright (c) 2002-2006 Oleksandr Shyshko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uwncorrector.client.application.gui.main.settings;

import uwncorrector.centre.gui.actionbinder.ActionBinder;
import uwncorrector.centre.gui.actionbinder.config.NListener;
import uwncorrector.centre.gui.resources.Resources;
import uwncorrector.client.application.gui.actions.Actions;
import uwncorrector.client.application.gui.main.settings.appearance.AppearanceGui;
import uwncorrector.client.application.gui.main.settings.behavior.BehaviorGui;
import uwncorrector.client.application.gui.main.settings.database.DatabaseGui;
import uwncorrector.client.application.gui.main.settings.dictionaries.DictionariesGui;
import uwncorrector.client.application.model.Model;
import uwncorrector.client.application.model.ModelAdapter;
import uwncorrector.client.application.model.Preferences;
import uwncorrector.client.application.util.Buttons;
import uwncorrector.client.application.util.Components;
import uwncorrector.client.application.util.Gaps;
import static uwncorrector.client.application.util.Gaps.GAP5;
import uwncorrector.client.application.util.messages.Messages;
import uwncorrector.client.application.util.misc.Threads;
import uwncorrector.client.application.util.progress.ProgressUtil;
import uwncorrector.client.application.util.progress.ITitledMonitor;
import uwncorrector.client.application.util.progress.engine.Monitors;
import uwncorrector.client.engine.IEngine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;

public class SettingsGui {
    private Resources resources = Resources.forProperties(getClass());

    private JFrame parentFrame;

    @NListener ( type = WindowListener.class, mappings = "windowClosing > hide" )
    private JDialog dialog;

    @NListener ( type = ActionListener.class, mappings = "actionPerformed > hide" )
    private JButton closeButton;
    private JTabbedPane tabbedPane;
    private Model model;
    private AppearanceGui appearanceGui;
    private DatabaseGui databaseGui;
    private BehaviorGui behaviorGui;
    private IEngine engine;

    public SettingsGui(Actions actions, final Model model, IEngine engine, JFrame parentFrame, DictionariesGui dictionariesGui, AppearanceGui appearanceGui, DatabaseGui databaseGui, BehaviorGui behaviorGui) {
        this.parentFrame = parentFrame;

        this.appearanceGui = appearanceGui;
        this.databaseGui = databaseGui;
        this.behaviorGui = behaviorGui;
        this.engine = engine;
        this.model = model;

        this.model.addApplicationModelListener(new ModelAdapter() {
            public void settings_show() {
                model.dropzone_hideTemporary();
                showDialogWithDefaults();
            }
        });

        closeButton = Buttons.close();

        /*JPanel southPanel = new JPanel( new TableLayout( new double[][] {
            { Gaps.GAP7, TableLayout.FILL, Gaps.GAP7, TableLayout.PREFERRED },
            { TableLayout.PREFERRED }
        } ) );
        southPanel.add( actions.getDownloadDictionariesAction().rollover(), "1, 0");
        southPanel.add( closeButton, "3, 0" );*/

        tabbedPane = new JTabbedPane();
        //tabbedPane.addTab(resources.text("database"), databaseGui.getGui());
        tabbedPane.addTab(resources.text("behavior"),  behaviorGui.getGui());
        tabbedPane.addTab(resources.text("appearance"),  appearanceGui.getGui());
        tabbedPane.addTab(resources.text("dictionaries"), dictionariesGui.getGui());

        JPanel panel = new JPanel(new BorderLayout(GAP5, GAP5 * 2));
        panel.add(tabbedPane, BorderLayout.CENTER);
        //panel.add( southPanel, BorderLayout.SOUTH );

        Gaps.applyBorder7(panel);

        dialog = Components.dialogModal(parentFrame);
        dialog.setTitle(resources.text("title"));
        dialog.setContentPane(panel);
        dialog.getRootPane().setDefaultButton(closeButton);

        ActionBinder.bind(this);
    }

    public void showDialogWithDefaults() {
        tabbedPane.setSelectedIndex( 0 );

        closeButton.requestFocusInWindow();

        dialog.setSize( 590, 450 );
        dialog.setLocationRelativeTo( parentFrame );
        dialog.setVisible( true );
    }

    public JDialog getDialog() {
        return dialog;
    }

    public void hide() {
        if ( engine.isCompiled() ) {
            hideAndUpdatePrefs();
            return;
        }

        final ITitledMonitor controller = ProgressUtil.start( dialog );

        Threads.startInBackground( new Runnable() {
            public void run() {
                try {
                    engine.compile( Monitors.compile( controller )  );

                    SwingUtilities.invokeLater( new Runnable() {
                        public void run() {
                            hideAndUpdatePrefs();
                        }
                    } );

                } catch ( Throwable t ) {
                    Messages.internalError( getDialog(), t );
                } finally {
                    ProgressUtil.stop( dialog );
                }
            }
        } );
    }

    private void hideAndUpdatePrefs() {
        dialog.setVisible(false);

        Preferences preferences = model.getPreferences();

        preferences.setAskBeforeNext(behaviorGui.getAskBeforeNext());
        preferences.setAutoGlossTransl(behaviorGui.getAutoGlossTransl());
        /*preferences.setUseFilterGlossLen(behaviorGui.getUseGlossFilter());
        preferences.setMaxGlossLen(behaviorGui.getMaxGlossLen());
        preferences.setUseFilterWordsCount(behaviorGui.getUseWordsFilter());
        preferences.setMaxWordsCount(behaviorGui.getMaxWordsCount());*/

        preferences.setFontSize(appearanceGui.getFontSize());
        preferences.setDropZoneVisible(appearanceGui.isDropZoneVisible());
        preferences.setMemoryBarVisible(appearanceGui.isMemoryBarVisible());
        preferences.setWelcomeLabel(appearanceGui.getWelcomeLabel());

/*        preferences.setDbIP(databaseGui.getDbIP());
        preferences.setDbPort(databaseGui.getDbPort());
        preferences.setDbSID(databaseGui.getDbSID());
        preferences.setDbDriver(databaseGui.getDbDriver());
        preferences.setDbUser(databaseGui.getDbUser());
        preferences.setDbPassword(databaseGui.getDbPassword());*/

        preferences.setAutoLogin(behaviorGui.isAutoLogin());
        preferences.setLogin(behaviorGui.getLogin());
        try {
            preferences.setPassword(behaviorGui.getPassword());
        }
        catch (Exception ex) { }

        model.settingsUpdated();
    }
}
